//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "CIISMain.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"

TCIISMainFrm *CIISMainFrm;
//---------------------------------------------------------------------------
__fastcall TCIISMainFrm::TCIISMainFrm(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TCIISMainFrm::FormCreate(TObject *Sender)
{
  ServerMode = -1;
  DB = NULL;
  NetInt = NULL;
  SerInt = NULL;
  TimerSysCheck->Enabled = false;
	BBReset->Enabled = false;
	InitFaild = false;

	StartDebugWin = true;

  #if Startmode == 0

	GroupBox1->Visible = false;
	
  StartDebugWin = false;
	RGServerMode->ItemIndex = 0;
  RGServerMode->Visible = false;
  CBAlias->Visible = false;
  GBLocalIP->Visible = false;
  GBPort->Visible = false;
	BQSetUp1->Visible = false;
	BQSetUp2->Visible = false;
  BBDebugWin->Visible = false;

  CIISMainFrm->WindowState = wsMinimized;

  #elif Startmode == 1

	GroupBox1->Visible = false;
	
	StartDebugWin = false;
  MEdRServerPortNo->Text = "5021";
  RGServerMode->ItemIndex = 1;
  RGServerMode->Visible = false;
  CBAlias->Visible = false;
  GBLocalIP->Visible = false;
  GBPort->Visible = false;
	BQSetUp1->Visible = false;
	BQSetUp2->Visible = false;
  BBDebugWin->Visible = false;

  CIISMainFrm->WindowState = wsMinimized;


  #elif Startmode == 2

	GroupBox1->Visible = false;

	StartDebugWin = false;
	RGServerMode->ItemIndex = 0;
  RGServerMode->Visible = false;
  CBAlias->Visible = false;
  GBLocalIP->Visible = false;
  GBPort->Visible = false;
	BQSetUp1->Visible = false;
	BQSetUp2->Visible = false;

	#elif Startmode == 3

	GroupBox1->Visible = false;
	
	//StartDebugWin = false;
  MEdRServerPortNo->Text = "5021";
  RGServerMode->ItemIndex = 1;
  RGServerMode->Visible = false;
  CBAlias->Visible = false;
  GBLocalIP->Visible = false;
  GBPort->Visible = false;
	BQSetUp1->Visible = false;
	BQSetUp2->Visible = false;

	CIISMainFrm->WindowState = wsMinimized;

  #endif

}
//---------------------------------------------------------------------------
void __fastcall TCIISMainFrm::FormDestroy(TObject *Sender)
{
  TimerSysCheck->Enabled = false;

  TSync = Now() + 10 * OneSecond;

  delete NetInt;
  delete SerInt;
  delete ClientInt;

  if( DB != NULL)
  {
		DB->TabProject->FindFirst();
    DB->TabProject->Edit();
		DB->TabProject->FieldValues["ServerStatus"] = CIISDisconnected;
    DB->TabProject->Post();

    DB->TabProject->Active = false;
    DB->TabControllers->Active = false;
    DB->TabZones->Active = false;
    DB->TabNodes->Active = false;
    DB->TabSensors->Active = false;
    DB->TabRecordings->Active = false;
    DB->TabValues->Active = false;
    DB->TabValuesLPR->Active = false;
    DB->TabValuesDecay->Active = false;
    DB->TabPowerSupply->Active = false;
    DB->TabEventLog->Active = false;
  }

  delete DB;
  delete DebWin;
  
}
//---------------------------------------------------------------------------
void __fastcall TCIISMainFrm::Debug(String Message)
{
  if( DebWin != NULL ) DebWin->Log(Message);
}
//---------------------------------------------------------------------------
void __fastcall TCIISMainFrm::BBResetClick(TObject *Sender)
{
	//LUSBConn->Caption = "USBCAN Disconnected";
	LNoOfBusInt->Caption = "0";
	if( !NetInt->ResetCanNet())
   #if DebugMain == 1
   Debug("Unable to start CIIS Net")
   #endif
   ;
}
//---------------------------------------------------------------------------
void __fastcall TCIISMainFrm::TimerSysCheckTimer(TObject *Sender)
{

	if( InitFaild ) CIISMainFrm->Close();

	if( DB->TabControllers->FindFirst() )
	{
		if( DB->TabProject->FieldByName("ServerStatus")->AsInteger == CIISDisconnected )
    {
			GBRecordings->Visible = false;
			GBCamurIILogger->Visible = false;
		}
		else
		{
			GBRecordings->Visible = true;
			GBCamurIILogger->Visible = true;
			LNoOfNodes->Caption = DB->TabControllers->FieldByName("NodeCount")->AsString;
			LNoOfMonitors->Caption = DB->TabControllers->FieldByName("MonitorCount")->AsString;
			LNoOfDecays->Caption = DB->TabControllers->FieldByName("DecayCount")->AsString;
			NoOfLPRs->Caption = DB->TabControllers->FieldByName("LPRCount")->AsString;
			if( DB->TabControllers->FieldByName("USBCANStatus")->AsInteger == 1 )
				LNoOfBusInt->Caption = "1"; //LUSBConn->Caption = "USBCAN Connected";
			else
				LNoOfBusInt->Caption = "0"; //LUSBConn->Caption = "USBCAN Disconnected";

		}

    if( ServerMode == CIISLocalServer )
    {
			GBClientPorts->Visible = false;//LLServer->Caption = "";

			if( ClientInt->LocalClientConnected )
				ShMonitorConnected->Brush->Color = clLime; //LLocalClient->Caption = "Logger Client Connected";
      else
				ShMonitorConnected->Brush->Color = clBtnFace; //LLocalClient->Caption = "Logger Client Disconnected";

			if( ClientInt->RemoteClientConnected )
				ShUserConnected->Brush->Color = clLime; //LRemotClient->Caption = "User Client Connected";
			else
				ShUserConnected->Brush->Color = clBtnFace; //LRemotClient->Caption = "User Client Disconnected";

			if( ClientInt->PMConnected )
				ShPMConnected->Brush->Color = clLime;
      else
				ShPMConnected->Brush->Color = clBtnFace;
		}
    else
    {
			LCIIUser->Visible = false;
			ShUserConnected->Visible = false; //   LRemotClient->Caption = "";

			if( ClientInt->LocalClientConnected )
				ShMonitorConnected->Brush->Color = clLime; //LLocalClient->Caption = "User Client Connected";
      else
				ShMonitorConnected->Brush->Color = clBtnFace; //LLocalClient->Caption = "User Client Disconnected";

			if( ClientInt->PMConnected )
				ShPMConnected->Brush->Color = clLime;
      else
				ShPMConnected->Brush->Color = clBtnFace;

			if( SerInt->Connected() )
      {
				ShLoggerConnected->Brush->Color = clLime; //LLServer->Caption = "Logger Server Connected";

        if( DB->TabControllers->FieldByName("USBCANStatus")->AsInteger == 1 )
					LNoOfBusInt->Caption = "1"; //LUSBConn->Caption = "USBCAN Connected";
        else
					LNoOfBusInt->Caption = "0"; //LUSBConn->Caption = "USBCAN Disconnected";
        if( TSync < Now() )
        {
          TSync = Now() + 10 * OneSecond;
					SerInt->SystemSyncIni();
        }

			}
      else
      {
				LNoOfBusInt->Caption = "0"; //LUSBConn->Caption = "";
				ShLoggerConnected->Brush->Color = clBtnFace; //LLLServer->Caption = "Logger Server Disconnected";
      }
    }
  }
}
//---------------------------------------------------------------------------
void __fastcall TCIISMainFrm::ChangeDebugState(int DState)
{
  if( DState == 0 )
  {
  DebWin = NULL;
  if(NetInt != NULL ) NetInt->SetDebugWin( NULL );
  if(SerInt != NULL ) SerInt->SetDebugWin( NULL );  
  if( ClientInt != NULL ) ClientInt->SetDebugWin( NULL );
  }
}
//---------------------------------------------------------------------------
void __fastcall TCIISMainFrm::RGServerModeClick(TObject *Sender)
{
	TimerSysCheck->Enabled = false;
  BBReset->Enabled = false;

  if( ServerMode != RGServerMode->ItemIndex + 1 )
	{
    if( DB != NULL )
    {
      DB->TabProject->FindFirst();
      DB->TabProject->Edit();
			DB->TabProject->FieldValues["ServerStatus"] = CIISDisconnected;
      DB->TabProject->Post();

      DB->TabProject->Active = false;
      DB->TabControllers->Active = false;
      DB->TabZones->Active = false;
      DB->TabNodes->Active = false;
      DB->TabSensors->Active = false;
      DB->TabRecordings->Active = false;
      DB->TabValues->Active = false;
      DB->TabValuesLPR->Active = false;
      DB->TabValuesDecay->Active = false;
      DB->TabPowerSupply->Active = false;
      DB->TabEventLog->Active = false;
    }

    delete NetInt;
    delete SerInt;
    delete ClientInt;
    delete DB;
    delete DebWin;

    DB = NULL;
    NetInt = NULL;
    SerInt = NULL;
    DebWin = NULL;


    switch (RGServerMode->ItemIndex + 1)
		{
      case CIISLocalServer:
      ServerMode = CIISLocalServer;
			break;

      case CIISRemoteServer:
			ServerMode = CIISRemoteServer;
			break;
    }

		if( StartDebugWin )
    {
			DebWin = new TDebugWin(this, &ChangeDebugState);
			DebWin->Show();
    }  

    DB = new TCIISDBModule(this);

		if( !DB->InitCIISDB( EdAlias->Text, ServerMode )) InitFaild = true;

		DB->TabProject->Active = true;
    DB->TabControllers->Active = true;
    DB->TabZones->Active = true;
		DB->TabNodes->Active = true;
    DB->TabSensors->Active = true;
    DB->TabRecordings->Active = true;
    DB->TabValues->Active = true;
    DB->TabValuesLPR->Active = true;
    DB->TabValuesDecay->Active = true;
    DB->TabPowerSupply->Active = true;
    DB->TabEventLog->Active = true;

    if( ServerMode == CIISLocalServer )
      NetInt = new TCIISNetInterface( DB, DebWin, "Controller 1" );
    else
      SerInt = new TCIISServerInterface( DB, DebWin, EdLocalIP->Text , UpDownClientPortNo->Position );

    ClientInt = new TCIISClientInterface( DB, DebWin, NetInt, SerInt, ServerMode, UpDownLServerPortNo->Position, UpDownLServerPortNoPM->Position, UpDownRServerPortNo->Position );

    DB->TabControllers->FindFirst();
    DB->TabProject->FindFirst();

    if( ServerMode == CIISLocalServer )
    {
      BBReset->Enabled = true;
      DB->TabProject->Edit();
			DB->TabProject->FieldValues["ServerStatus"] = CIIDataSync;
      DB->TabProject->Post();
      if( !NetInt->ResetCanNet())
      {
        #if DebugMain == 1
        Debug("Unable to start CIIS Net");
        #endif
				LNoOfBusInt->Caption = "-"; //LUSBConn->Caption = "USBCAN: Off";
      }
      else LNoOfBusInt->Caption = "1"; //LUSBConn->Caption = "USBCAN: On";
    }
    else
    {
      BBReset->Enabled = false;
      DB->TabProject->Edit();
			DB->TabProject->FieldValues["ServerStatus"] = CIISDisconnected;
      DB->TabProject->Post();
    }

    DB->Log( SystemEvent, ServerStart, High, "ServerStart", 0, 0 );
    TimerSysCheck->Enabled = true;
  }
}
//---------------------------------------------------------------------------
void __fastcall TCIISMainFrm::BBDebugWinClick(TObject *Sender)
{

  DebWin = new TDebugWin(this, &ChangeDebugState);
  DebWin->Show();

  if(NetInt != NULL ) NetInt->SetDebugWin( DebWin );
  if(SerInt != NULL ) SerInt->SetDebugWin( DebWin );
  if( ClientInt != NULL ) ClientInt->SetDebugWin( DebWin );

}
//---------------------------------------------------------------------------
void __fastcall TCIISMainFrm::BQSetUp1Click(TObject *Sender)
{
/*
	EdLocalIP->Text = "62.101.207.111";
	MEdRServerPortNo->Text = "5021";
*/
	MEdLServerPortNoPM->Text = "5016";
	MEdLServerPortNo->Text = "5017";
	MEdClientPortNo->Text = "5020";
	EdAlias->Text = "CIISLogger";
	
}
//---------------------------------------------------------------------------
void __fastcall TCIISMainFrm::FormShow(TObject *Sender)
{
	CIISMainFrm->Caption = "Camur II Server Ver 1.2.0.1";// + GetProgVersion();
	//CIISMainFrm->Caption = "Camur II Server Ver " + GetProgVersion();
}
//---------------------------------------------------------------------------



void __fastcall TCIISMainFrm::BQSetUp2Click(TObject *Sender)
{
	//MEdLServerPortNoPM->Text = "5016";
	//MEdLServerPortNo->Text = "5017";
	MEdRServerPortNo->Text = "5021";
	EdAlias->Text = "CIISUser";
}
//---------------------------------------------------------------------------




